package org.zsk.cs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zsk.cs.common.AppUtils;
import org.zsk.cs.dao.IdentityAuthDao;
import org.zsk.cs.dao.UserDao;
import org.zsk.cs.domain.po.Forum;
import org.zsk.cs.dao.ForumDao;
import org.zsk.cs.domain.po.IdentityAuth;
import org.zsk.cs.domain.po.User;
import org.zsk.cs.domain.vo.ForumVo;
import org.zsk.cs.service.ForumService;
import org.zsk.fastdev.assign.Assign;
import org.zsk.fastdev.bean.BeanUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * <p>
 * 社区论坛 serviceImpl
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/03/26
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ForumServiceImpl extends ServiceImpl<ForumDao, Forum> implements ForumService {

    private final ForumDao forumDao;
    private final IdentityAuthDao identityAuthDao;
    private final UserDao userDao;
    private final Assign assign;
    private final AppUtils appUtils;

    @Autowired
    public ForumServiceImpl(ForumDao forumDao, IdentityAuthDao identityAuthDao, UserDao userDao, Assign assign, AppUtils appUtils) {
        this.forumDao = forumDao;
        this.identityAuthDao = identityAuthDao;
        this.userDao = userDao;
        this.assign = assign;
        this.appUtils = appUtils;
    }

    @Override
    public boolean save(Forum entity) {
        entity.setCode(UUID.randomUUID().toString().replace("-",""));
        return super.save(entity);
    }

    @Override
    public Boolean addByDistrictHead(Forum forum) {
        this.save(forum);
        //websocket 开发中
        return true;
    }

    @Override
    public Boolean residentAccess(Forum forum) {
        return true;
    }

    @Override
    public List<Forum> search(ForumVo forumVo) throws Exception {
        Forum forum=new Forum();
        if (Objects.nonNull(appUtils.getDistrictHead())){
            forumVo.setDistrictHeadId(appUtils.getDistrictHead().getId());
        }else if (Objects.nonNull(appUtils.getCurResident())){
            forumVo.setDistrictHeadId(identityAuthDao.selectDistrictHeadIdByResidentId(appUtils.getCurResident().getId()));
        }
        assign.assign(forumVo,forum,"districtHeadId,status");
        //根据社区长id搜索
        return forumDao.selectList(BeanUtils.bean2QueryWrapper(Forum.class,forum));
    }

    @Override
    public Page<ForumVo> getWithUserNameByPage(ForumVo forumVo) throws Exception {
        Page<Forum> page=new Page<>();
        assign.assign(forumVo,page,"$current,$size");
        Page<Forum> forumPage=page(page);
        return getForumVoPage(forumPage);
    }

    @Override
    public Page<ForumVo> districtHeadGetWithUserNameByPage(ForumVo forumVo) throws Exception {
        Page<Forum> page=new Page<>();
        assign.assign(forumVo,page,"$current,$size");
        Page<Forum> forumPage=page(page,new LambdaQueryWrapper<Forum>()
                .eq(Forum::getDistrictHeadId,appUtils.getDistrictHead().getId()));
        return getForumVoPage(forumPage);
    }

    @Override
    public Page<ForumVo> residentGetWithUserNameByPage(ForumVo forumVo) throws Exception {
        Page<Forum> page=new Page<>();
        assign.assign(forumVo,page,"$current,$size");
        Page<Forum> forumPage=page(page,new LambdaQueryWrapper<Forum>()
                .eq(Forum::getDistrictHeadId,identityAuthDao.
                        selectDistrictHeadIdByResidentId(appUtils.getCurResident().getId())));
        return getForumVoPage(forumPage);
    }

    private Page<ForumVo> getForumVoPage(Page<Forum> forumPage) throws Exception {
        Page<ForumVo> forumVoPage = new Page<>();
        //合并搜索结果
        List<ForumVo> forumVoList = forumPage.getRecords().stream().collect(LinkedList::new, (curForumVoList, forum) -> {
            IdentityAuth districtHead = identityAuthDao.selectById(forum.getDistrictHeadId());
            User user = userDao.selectById(districtHead.getUid());
            ForumVo curForumVo = new ForumVo();
            try {
                assign.assign(forum, curForumVo);
                curForumVo.setUsername(user.getUsername());
                curForumVoList.add(curForumVo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, LinkedList::addAll);
        assign.assign(forumPage, forumVoPage);
        forumVoPage.setRecords(forumVoList);
        return forumVoPage;
    }

    @Override
    public boolean districtHeadSave(ForumVo forumVo) throws Exception {
        Forum forum=new Forum();
        forum.setDistrictHeadId(appUtils.getDistrictHead().getId());
        assign.assign(forumVo,forum,"$title");
        forum.setCode(UUID.randomUUID().toString().replace("-",""));
        return super.save(forum);
    }
}
